﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectModel.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace ViewComponents
{
    public partial class GetRangeForm : Form
    {
        private RangeTextBoxWrapper _txtBox;
        private LinkedRange _linkedRng;
        private Excel.Range _selRng;

        public GetRangeForm(LinkedRange range, Excel.Application app)
        {
            InitializeComponent();

            // Wrap controls
            _txtBox = new RangeTextBoxWrapper(this.rngTextBox, app);

            // Initialise fields
            _linkedRng = range;

            _selRng = app.Selection as Excel.Range;
        }

        private void doneBtn_Click(object sender, EventArgs e)
        {
            this.SetLinkedRangeSource();
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            this.RestoreSelection();
        }

        private void RestoreSelection()
        {
            if (_selRng != null)
            {
                Excel.Workbook wb = _selRng.Worksheet.Parent;
                ((Excel._Workbook)wb).Activate();
                _selRng.Select();
            }
        }

        private void SetLinkedRangeSource()
        {
			_linkedRng.SourceRange.Range = _selRng;
        }
    }

    public class RangeTextBoxWrapper
    {
        private Excel.Application _app;
        private Excel.Range _selRng;
        private TextBox _control;
        
        public RangeTextBoxWrapper(TextBox control, Excel.Application app)
        {
            _control = control;
            _control.TextChanged += this.ControlTextChanged;

            // Initialise control
            _selRng = app.Selection as Excel.Range;
            if (_selRng != null)
                this.UpdateText(_selRng);

            _app = app;
            _app.SheetSelectionChange += this.AppSheetSelectionChange;
        }

        public Excel.Range SelectedRange
        {
            get { return _selRng; }

            set
            {
                _selRng = value;
                
                _app.SheetSelectionChange -= this.AppSheetSelectionChange;
                _selRng.Select();
                _app.SheetSelectionChange += this.AppSheetSelectionChange;
            }
        }

        public string Text
        {
            get { return _control.Text; }

            set
            {
                _control.TextChanged -= this.ControlTextChanged;
                _control.Text = value;
                _control.TextChanged += this.ControlTextChanged;
            }
        }

        private void AppSheetSelectionChange(object Sh, Excel.Range Target)
        {
            _selRng = Target;
            this.UpdateText(Target);
        }

        private void ControlTextChanged(object sender, EventArgs e)
        {
            
        }

        private void UpdateText(Excel.Range rng)
        {
            
        }
    }
}
